<template>
  <div class="kw-form-item kw-table-column-prop" :class="config.cls">
    <div class="kw-form-item-label">
      <span>{{config.name}}</span>
      <span class="desc">{{config.desc}}</span>
    </div>
    <div class="kw-form-item-content">
      <div v-bind:class="{limitdel:defaultValue.length<=1,limitadd:defaultValue.length>=200}">
        <div v-for="(item, index) in defaultValue" class="kw-form-option" :key="index">
          <input type="text" style="width: 30%; margin-right: 5px;" v-model="item.title" placeholder="标题">
          <input type="text" style="width: 20%; margin-right: 5px;" v-model="item.width" placeholder="宽度">
          <kw-select style="width: 28%; margin-right: 5px;" v-model="item.textAlign" :options="textAligns" placeholder="对齐方式"></kw-select>
          <a @click="(e)=>{del(e, item, index)}" class="action action-del">
            <i class="iconfont icon-minus"></i>
          </a>
        </div>
        <a @click="add2" class="action action-add">
          <i class="iconfont icon-plus"></i>
        </a>
      </div>
    </div>
  </div>
</template>

<script>
import { prop } from '../mixins/prop'
export default {
  name: 'table-column-prop',
  mixins: [prop],
  props: {
    setting: {
      default() {
        return {
          columns: []
        }
      }
    },
    config: {
      default: {
        prop: 'columns'
      }
    }
  },
  data() {
    return {
      defaultValue: this.setting.columns || [],
      textAligns: [
        {label: '左', value: 'left'},
        {label: '中', value: 'center'},
        {label: '右', value: 'right'}
      ]
    }
  },
  methods: {
    add2(e) {
      e.stopPropagation()
      e.preventDefault()
      this.defaultValue.push({title: '', width: '', children: []})
    },
    add(e, item, index) {
      e.stopPropagation()
      e.preventDefault()
      this.defaultValue.splice(index + 1, 0, {title: '', width: '', children: []})
    },
    del(e, item, index) {
      e.stopPropagation()
      e.preventDefault()
      this.defaultValue.splice(index, 1)
    }
  }
}
</script>

<style lang="less">
.kw-table-column-prop {
  .limitadd .action-add,
  .limitdel .action-del {
    opacity: 0.2;
    pointer-events: none;
  }
  .action {
    .iconfont {
      background-color: #3c3435;
      border-radius: 50%;
      color: #fff;
      padding: 2px;
      margin: 0 3px;
      &:hover {
        background: #000;
      }
    }
  }
  .action-del {
    .iconfont {
      background-color: #ff5400;
      &:hover {
        background: red;
      }
    }
  }
}
</style>